package com.yzg.cas;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @author misterWei
 * @create 2020年05月24号:13点43分
 * @mailbox forcontinue@163.com
 */
@Slf4j(topic = "c.CasAbA")
public class CasAbA {

    public static void main(String[] args) throws InterruptedException {
        AtomicReference<String> a = new AtomicReference("A");
        String perv = a.get();
        String next = "C";
        //ABA 问题发生
        options(a);
        Thread.sleep(1000L);
        a.compareAndSet(perv,next);
        log.info(" 该值为: {}",a.get());
    }

    static void options( AtomicReference<String> a){
        new Thread(()->{
            String perv = a.get();
            String next = "B";
            a.compareAndSet(perv,next);
            log.info(" 该值为: {}",a.get());
        },"t1").start();

        new Thread(()->{
            String perv = a.get();
            String next = "A";
            a.compareAndSet(perv,next);
            log.info(" 该值为: {}",a.get());
        },"t1").start();

    }
}
